import { Component, OnInit, Injector, Input, Output, EventEmitter } from '@angular/core';
import { Http } from '@angular/http';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from 'app/server/server';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-resource-type-list',
  templateUrl: './resource-type-list.component.html',
  styleUrls: ['./resource-type-list.component.css']
})
export class ResourceTypeListComponent implements OnInit {

  protected cudSrv: CudTableService
  protected srv: Service
  protected route: ActivatedRoute
  constructor(
    private injector: Injector
  ) {
    this.cudSrv = injector.get(CudTableService)
    this.srv = injector.get(Service)
    this.route = injector.get(ActivatedRoute)
  }
  @Input() id
  @Output() idChange = new EventEmitter<string>();
  resType: string
  ngOnInit() {
    this.resType = this.route.snapshot.url[0]["path"]
    this.cudSrv.postRequest(
      this.srv.baseUrl + 'product/resouceType/getList',
      { pageNumber: 1, pageSize: 20, queryType: 3, queryCriteria: "" }).then(res => {
        if (res.result_code == "9999") {
          throw ("<app-resource-type-list/>获取数据失败")
        }
        if (res.result_code == '0000') {
          this.resourceTypeList = res.result;
          if (this.resourceTypeList instanceof Array && this.resourceTypeList.length) {
            this.resourceTypeList.push({
              description: '全部',
              enName: "all",
              name: "全部",
              resTypeId: ""
            })
          }
          for (let item of this.resourceTypeList) {
            let reg = new RegExp(item.enName.toLowerCase(), "g")
            if (reg.test(this.resType) || (this.resType == 'periodical' && item.enName == "Magazine")) {
              this.id = item.resTypeId
              this.idChange.emit(this.id)
              return item.resTypeId
            }
          }
        }
      })
  }
  resourceTypeList: Array<any>
  change() {
    this.idChange.emit(this.id)
  }
}
